package net.wzw.mq.consumer;

import net.wzw.constant.RocketMQConstant;
import net.wzw.mq.RocketMQProperties;
import net.wzw.mq.consumer.listener.FoodUpdateListener;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wzw
 * @date 2025/4/21
 * @Description 消费者bean 启动消费者
 */
@Configuration
public class ConsumerBeanConfig {

    /**
     * 配置内容对象
     * **/
    @Autowired
    private RocketMQProperties rocketMQProperties;

    /**
     * 美食信息变更消费者
     * **/
    @Bean("foodAsyncUpdateTopic")
    public DefaultMQPushConsumer foodAsyncUpdateTopic(FoodUpdateListener  foodUpdateListener) throws MQClientException {

        //实例化消费者客户端
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMQConstant.FOOD_DEFAULT_CONSUMER_GROUP);

        //设置nameserver 地址
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());

        //订阅 topic
        consumer.subscribe(RocketMQConstant.FOOD_UPDATE_MESSAGE_TOPIC,"*");

        //注册消息监听器
        consumer.registerMessageListener(foodUpdateListener);

        //启动消费者
        consumer.start();

        return consumer;

    }



}
